package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: DishServiceImpl
 * @Author 贾金博
 * @Package com.sky.service.impl
 * @Date 2024/3/25 18:48
 * @description:
 */
@Service
@Slf4j

public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增菜品 和 对应的口味数据
     * @param dishDTO
     */
    @Override
    @Transactional // 因为是多张表格 口味表 菜品表  要么全成功  要么全失败
    public void saveWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // 向 菜品表 插入数据 1条数据
        dishMapper.insert(dish);

        Long id = dish.getId(); // 会把插入数据的id给带回来
        // 向 口味表 插入数据 多条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors == null || flavors.size() == 0) {

        } else {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize()); // 开始分页
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult()); // 固定的
    }

    /**
     * 菜品批量删除
     * @param ids
     */
    @Override
    @Transactional // 因为涉及到多表嘛  需要加上事物的注解 要成功都成功 要失败都失败
    public void deleteBatch(List<Long> ids) {
       // 当前菜品是否能够删除 -- 时候纯在起售中的菜品
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE) { // 表示的是起售
                //当前这个菜品处于起售 不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        // 当前这个菜品被某个套餐关联了
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds != null && setmealIds.size() > 0 ) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 可以删除 删除菜品表中的菜品数据
//        for (Long id : ids) {
//            dishMapper.deleteById(id);
//            // 删除口味数据(相互关联的数据)
//            dishFlavorMapper.deleteByDishId(id);
//        }
        //根据菜品id集合 批量删除菜品数据
        dishMapper.deleteByIds(ids);
        //根据菜品id集合 批量删除关联的口味id
        dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     * 根据id查询菜品和对应的口味数据
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //根据id查询菜品数据
        Dish dish = dishMapper.getById(id);
        // 根据菜品id查询口味数据
        List<DishFlavor>dishFlavors = dishFlavorMapper.getByDishId(id);
        DishVO dishVO = new DishVO();
        //对象拷贝
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 根据id修改菜品基本信息 以及口味信息
     * @param dishDTO
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        // 修改菜品的基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        //口味修改的时候 比较麻烦
        // 先把之前的口味全部给删除掉
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //然后再把口味给重新插入
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors == null || flavors.size() == 0) {

        } else {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 修改菜品的售卖状态
     * @param status
     * @param id
     */
    @Override
    public void status(int status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
        //修改菜品状态的时候 需要考虑到和他相关联的菜品套餐
        //如果是把这个菜品给停掉的时候 也要把套餐给停到
        if(status == StatusConstant.DISABLE) {
            //先去得到套餐的id
            List<Long>dishIds = new ArrayList<>();
            dishIds.add(id);

            List<Long> setmealIdsByDishIds = setmealDishMapper.getSetmealIdsByDishIds(dishIds);
            if(setmealIdsByDishIds != null && setmealIdsByDishIds.size() > 0 ) {
                for(Long setmealId : setmealIdsByDishIds) {
                    Setmeal setmeal = new Setmeal();
                    setmeal.setId(setmealId);
                    setmeal.setStatus(0); // 停售
                    setmealMapper.update(setmeal);
                }
            }
        }
    }

    /**
     * 根据菜品分类id去查询菜品
     * @param dish
     * @return
     */
    @Override
    public List<Dish> findDishesByCategoryId(Dish dish) {
        List<Dish> dishes = dishMapper.findDishesByCategoryId(dish);
        return dishes;
    }


    /**
     * 根据菜品名字去查询菜品
     * @param name
     * @return
     */
    @Override
    public List<Dish> findDishesByName(String name) {
        List<Dish> dishes = dishMapper.findDishesByName(name);
        return dishes;
    }



    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }
}
